/* Class: UWA.Environments.Standalone

_Copyright 2006-2007 Netvibes. All rights reserved._

*/

Object.extend(UWA.Environment.prototype, {
  
initialize: function() {
  this.standalone = {};
},

onInit: function() {

  // creating module header
  var moduleHeader = document.createElement('div');
  moduleHeader.setAttribute('id','moduleHeader');
  moduleHeader.className = 'moduleHeader';
  moduleHeader.innerHTML = 
      '<div class="edit"><a href="#">Edit</a></div>' +
      //'<div class="refresh"><img src="http://www.netvibes.com/api/0.3/refresh.gif"></div>' +
      '<div class="ico" id="moduleIcon" style="padding-left:3px">&nbsp;</div>' +
      '<div id="moduleTitle" class="title">' + document.title + '</div>';
  
  // creating module content
  var moduleContent = document.createElement('div');
  moduleContent.setAttribute('id','moduleContent');
  moduleContent.className = 'moduleContent';
  moduleContent.innerHTML = document.body.innerHTML ;
  
  // creating edit content
  var editContent = document.createElement('div');
  editContent.setAttribute('id','editContent');
  editContent.style.display = 'none';
  editContent.className = 'editContent optionContent configureContent';
  
  document.body.innerHTML = '';
  
  var wrapper = document.createElement('div');
  wrapper.setAttribute('id','wrapper');

  this.html['header'] = wrapper.appendChild(moduleHeader);
  this.html['edit'] = wrapper.appendChild(editContent);
  this.html['body'] = wrapper.appendChild(moduleContent);

  document.body.appendChild(wrapper);
  
  this.html['title'] = document.getElementById('moduleTitle'); // to refactor
  this.html['icon'] = document.getElementById('moduleIcon'); // to refactor
  
  var addto = document.createElement('div');
  addto.setAttribute('id','addto');
  
  addto.innerHTML =  '<a style="border:0" title="Add this module to Netvibes" href="' + NV_PATH + 'subscribe.php?module=UWA&amp;moduleUrl=' + encodeURIComponent(document.location.href) + '"><img alt="Add to Netvibes" src="' + NV_PATH + 'img/uwa-netvibes.png" /></a>';
  
  addto.innerHTML +=  '<br /> <br /><a style="border:0" title="Add this module to Google Homepage" href="http://www.google.com/ig/add?moduleurl=' + encodeURIComponent(NV_PATH + 'api/uwa/compile/google.php?moduleUrl=' +  encodeURIComponent(document.location.href) ) + '"><img alt="Add to Google Homepage" src="' + NV_PATH + 'img/uwa-google.png" /></a>';

  document.body.appendChild(addto);
    
},

onRegisterModule: function() {
  
  for (var key in this.html) {
    this.module.elements[key] = UWA.$element(this.html[key]);
  }
  
  // new syntax
  this.module.body = this.module.elements['body'];
  
  var editLink = this.module.elements['header'].getElementsByClassName('edit')[0];
  editLink.onclick = ( function() {
    if (this.elements['edit'].style.display == 'none') {
      this.callback('onEdit');
    } else {
      this.elements['edit'].hide();
    }
    return false;
  } ).bindAsEventListener(this.module);
  
  /*
  var refreshLink = this.$('header').getElementsByClassName('refresh')[0];
  refreshLink.onclick = ( function() {
    this.callback('onRefresh')
  } ).bindAsEventListener(this.module);
  */
  
  var xmlMetas = document.getElementsByTagName("meta");
  if(xmlMetas && xmlMetas.length) this.module.setMetasXML(xmlMetas);
  
  var xmlPrefs = document.getElementsByTagName("preference");
  if (xmlPrefs && xmlPrefs.length) this.module.setPreferencesXML(xmlPrefs);
  
  var links = document.getElementsByTagName('link');
  for(var i = 0; i < links.length; i++) {
    if(links[i].getAttribute('rel') == 'icon') {
      var favicon = links[i].getAttribute('href');
    }
  }
  if(favicon) {
    // var buildHref = UWA.Utils.buildUrl(this.widget.getValue('moduleUrl'), favicon);
    // if(buildHref) { favicon = buildHref }
    this.setIcon(favicon);
  }
    
  var launchModule = setTimeout(this.launchModule.bind(this), 100);
     
},

showEdit: function() {
  if(this.module.onEdit) this.module.onEdit();
},

getData: function(name) {
  if(window.Cookie) return Cookie.get('uwa-' + name);
},

setData: function(name, value) {
  if(window.Cookie) return Cookie.set('uwa-' + name, value);
}

});

var Environment = new UWA.Environment();

var widget = Environment.getModule();

UWA.Data.useJsonRequest = true;

function _(s) {
  return s
}

window.onresize = function() {
  widget.callback('onResize');
}

UWA.proxies['feed'] = NV_PATH + 'data/feed/';